package com.hp.bon.sgw.wh.bean;

import java.sql.Timestamp;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.hp.bon.sgw.domain.BusinessRecord;
import com.hp.bon.sgw.util.Comutils;

@Entity
@Table(name = "T_BUSINESS_RECORD_PORT")
public class BusinessRecordPort {
	private int id;
	private String businessCallId;
	private String sessionId;
	private String serviceContextId;
	private String requestHost;
	private String requestIP;
	private int fromNodeId;
	private int toNodeId;
	private int protocolType;
	private int transDirection;
	private Timestamp requestTime;
	private String responseTime;
	private int result;
	private String resultReason;
	private long recvRequestTime;
	private long sendRequestTime;
	private long recvResponseTime;
	private long sendResponseTime;
	private int recvRequestLength;
	private int sendRequestLength;
	private int recvResponseLength;
	private int sendResponseLength;
	private String responseHost;
	private Integer resultCode;
	private Integer serviceResultCode;
	private Integer answerValidateCode = Integer.valueOf(-1);
	private String validateDesc;
	public static int seq = 0;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	public int getId() {
		return this.id;
	}

	@Column(name = "BUSINESS_CALL_ID")
	public String getBusinessCallId() {
		return this.businessCallId;
	}

	@Column(name = "SESSION_ID")
	public String getSessionId() {
		return this.sessionId;
	}

	@Column(name = "SERVICE_CONTEXT_ID")
	public String getServiceContextId() {
		return this.serviceContextId;
	}

	@Column(name = "REQUEST_HOST")
	public String getRequestHost() {
		return this.requestHost;
	}

	@Column(name = "REQUEST_IP")
	public String getRequestIP() {
		return this.requestIP;
	}

	@Column(name = "FROM_NODE_ID")
	public int getFromNodeId() {
		return this.fromNodeId;
	}

	@Column(name = "TO_NODE_ID")
	public int getToNodeId() {
		return this.toNodeId;
	}

	@Column(name = "PROTOCOL_TYPE")
	public int getProtocolType() {
		return this.protocolType;
	}

	@Column(name = "TRANS_DIRECTION")
	public int getTransDirection() {
		return this.transDirection;
	}

	@Column(name = "RESULT_CODE")
	public Integer getResultCode() {
		return this.resultCode;
	}

	@Column(name = "SERVICE_RESULT_CODE")
	public Integer getServiceResultCode() {
		return this.serviceResultCode;
	}

	@Column(name = "RESULT_REASON")
	public String getResultReason() {
		return this.resultReason;
	}

	@Column(name = "REQUEST_TIME")
	public Timestamp getRequestTime() {
		return this.requestTime;
	}

	@Column(name = "RESPONSE_TIME")
	public String getResponseTime() {
		return this.responseTime;
	}

	@Column(name = "RECV_REQUEST_TIME")
	public long getRecvRequestTime() {
		return this.recvRequestTime;
	}

	@Column(name = "SEND_REQUEST_TIME")
	public long getSendRequestTime() {
		return this.sendRequestTime;
	}

	@Column(name = "RECV_RESPONSE_TIME")
	public long getRecvResponseTime() {
		return this.recvResponseTime;
	}

	@Column(name = "SEND_RESPONSE_TIME")
	public long getSendResponseTime() {
		return this.sendResponseTime;
	}

	@Column(name = "RECV_REQUEST_LENGTH")
	public int getRecvRequestLength() {
		return this.recvRequestLength;
	}

	@Column(name = "SEND_REQUEST_LENGTH")
	public int getSendRequestLength() {
		return this.sendRequestLength;
	}

	@Column(name = "RECV_RESPONSE_LENGTH")
	public int getRecvResponseLength() {
		return this.recvResponseLength;
	}

	@Column(name = "SEND_RESPONSE_LENGTH")
	public int getSendResponseLength() {
		return this.sendResponseLength;
	}

	@Column(name = "RESPONSE_HOST")
	public String getResponseHost() {
		return this.responseHost;
	}

	@Column(name = "ACK_VALIDATE_CODE")
	public Integer getAnswerValidateCode() {
		return this.answerValidateCode;
	}

	@Column(name = "ACK_VALIDATE_DESC")
	public String getValidateDesc() {
		return this.validateDesc;
	}

	public void setAnswerValidateCode(Integer answerValidateCode) {
		this.answerValidateCode = answerValidateCode;
	}

	public void setValidateDesc(String validateDesc) {
		this.validateDesc = validateDesc;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setBusinessCallId(String businessCallId) {
		this.businessCallId = businessCallId;
	}

	public void setSessionId(String sessionId) {
		this.sessionId = sessionId;
	}

	public void setServiceContextId(String serviceContextId) {
		this.serviceContextId = serviceContextId;
	}

	public void setRequestHost(String requestHost) {
		this.requestHost = requestHost;
	}

	public void setRequestIP(String requestIP) {
		this.requestIP = requestIP;
	}

	public void setFromNodeId(int fromNodeId) {
		this.fromNodeId = fromNodeId;
	}

	public void setToNodeId(int toNodeId) {
		this.toNodeId = toNodeId;
	}

	public void setProtocolType(int protocolType) {
		this.protocolType = protocolType;
	}

	public void setTransDirection(int transDirection) {
		this.transDirection = transDirection;
	}

	public void setResultCode(Integer resultCode) {
		this.resultCode = resultCode;
	}

	public void setServiceResultCode(Integer serviceResultCode) {
		this.serviceResultCode = serviceResultCode;
	}

	public void setResultReason(String resultReason) {
		this.resultReason = resultReason;
	}

	public void setRequestTime(Timestamp requestTime) {
		this.requestTime = requestTime;
	}

	public void setResponseTime(String responseTime) {
		this.responseTime = responseTime;
	}

	public void setRecvRequestTime(long recvRequestTime) {
		this.recvRequestTime = recvRequestTime;

		setRequestTime(new Timestamp(recvRequestTime));
	}

	public void setSendRequestTime(long sendRequestTime) {
		this.sendRequestTime = sendRequestTime;
	}

	public void setRecvResponseTime(long recvResponseTime) {
		this.recvResponseTime = recvResponseTime;
	}

	public void setSendResponseTime(long sendResponseTime) {
		this.sendResponseTime = sendResponseTime;
		setResponseTime(String.valueOf(sendResponseTime));
	}

	public void setRecvRequestLength(int recvRequestLength) {
		this.recvRequestLength = recvRequestLength;
	}

	public void setSendRequestLength(int sendRequestLength) {
		this.sendRequestLength = sendRequestLength;
	}

	public void setRecvResponseLength(int recvResponseLength) {
		this.recvResponseLength = recvResponseLength;
	}

	public void setSendResponseLength(int sendResponseLength) {
		this.sendResponseLength = sendResponseLength;
	}

	public void setResponseHost(String responseHost) {
		this.responseHost = responseHost;
	}

	public int getResult() {
		return this.result;
	}

	public void setResult(int result) {
		this.result = result;
	}

	public String toString() {
		return this.businessCallId + "||||" + this.sessionId + "||||"
				+ this.serviceContextId + "||||" + this.requestHost + "||||"
				+ this.requestIP + "||||" + this.fromNodeId + "||||"
				+ this.toNodeId + "||||" + this.protocolType + "||||"
				+ this.transDirection + "||||" + this.result + "||||"
				+ this.resultReason + "||||" + this.recvRequestTime + "||||"
				+ this.sendRequestTime + "||||" + this.recvResponseTime
				+ "||||" + this.sendResponseTime + "||||"
				+ this.recvRequestLength + "||||" + this.sendRequestLength
				+ "||||" + this.recvResponseLength + "||||"
				+ this.sendResponseLength + "||||" + this.responseHost + "||||"
				+ this.resultCode + "||||" + this.serviceResultCode + "||||"
				+ this.answerValidateCode + "||||" + this.validateDesc;
	}

	public static BusinessRecord convertToObject(String str) {
		String[] values = str.split("\\|\\|\\|\\|");
		BusinessRecord br = new BusinessRecord();
		br.setBusinessCallId(values[0]);
		br.setSessionId(values[1]);
		br.setServiceContextId(values[2]);
		br.setRequestHost(values[3]);
		br.setRequestIP(values[4]);
		br.setFromNodeId(getInt(values[5]));
		br.setToNodeId(getInt(values[6]));
		br.setProtocolType(getInt(values[7]));
		br.setTransDirection(getInt(values[8]));
		br.setResult(getInt(values[9]));
		br.setResultReason(values[10]);
		br.setRecvRequestTime(getLong(values[11]));
		br.setSendRequestTime(getLong(values[12]));
		br.setRecvResponseTime(getLong(values[13]));
		br.setSendResponseTime(getLong(values[14]));
		br.setRecvRequestLength(getInt(values[15]));
		br.setSendRequestLength(getInt(values[16]));
		br.setRecvResponseLength(getInt(values[17]));
		br.setSendResponseLength(getInt(values[18]));
		br.setResponseHost(values[19]);

		br.setResultCode(getInteger(values[20]));
		br.setServiceResultCode(getInteger(values[21]));

		br.setAnswerValidateCode(Integer.valueOf(getInt(values[22])));
		br.setValidateDesc(values[23]);
		return br;
	}

	private static int getInt(String value) {
		try {
			return Integer.valueOf(value).intValue();
		} catch (Exception localException) {
		}
		return -1;
	}

	private static Integer getInteger(String value) {
		try {
			return Integer.valueOf(value);
		} catch (Exception localException) {
		}
		return null;
	}

	private static long getLong(String value) {
		try {
			return Long.valueOf(value).longValue();
		} catch (Exception localException) {
		}
		return -1L;
	}

	@Transient
	public static String getSynSeq() {
		StringBuilder result = new StringBuilder(getSeq());
		int len = result.length();
		for (int i = len; i <= 8; ++i) {
			result.insert(0, "0");
		}
		return Comutils.formatTime(System.currentTimeMillis(),
				"yyyyMMddHHmmssSSS") + result.toString();
	}

	@Transient
	private static synchronized int getSeq() {
		if (seq == 99999999) {
			seq = 0;
		}
		return (++seq);
	}
}
